

*================================
*SETTINGS
*================================
set more off
set trace off
*set trace on 
*set tracedepth 2
set scheme s1mono
set linesize 255
set matsize 2000
cap log close _all


*================================
*READ IN DATA SSI/SSDI
*================================


*CPI data
use "./raw/cpi.dta", clear
rename year calendar_year
tempfile cpi
save `cpi', replace

*CLEAN SSDI
use "./raw/ssdi_2017_import_v1_nopii.dta", clear
keep pid dib*
*EXTRAPOLATE YEARS 2015 years to 2016-2018
foreach x in dib_aux_tot dib_prim_tot {
forvalues i=2016/2018 {
gen `x'`i'=`x'2015
}
}
reshape long dib_aux_tot dib_prim_tot, i(pid) j(calendar_year)
g dib_tot = dib_aux_tot+dib_prim_tot
g any_dib = (dib_prim_tot>0 & dib_prim_tot< .) | (dib_aux_tot>0 & dib_aux_tot <  .)
*INFLATE NEW VALUES
merge m:1 calendar_year using `cpi', keep(3) nogen
foreach x in dib_aux_tot dib_prim_tot dib_tot {
replace `x'=`x'*(cpiaucsl/236.9978) if inrange(calendar_year,2016,2018)
}


save "./processed/SSDIpanel_cleaned_extrapolated", replace


*CLEAN SSI
use "./raw/ssi_2017_import_v3_nopii.dta", clear
drop fedpmt_2016 statpmt_2016
foreach x in fedpmt_ statpmt_ {
forvalues i=2016/2018 {
gen `x'`i'=`x'2015
}
}

reshape long fedpmt_ statpmt_, i(pid) j(calendar_year)
g ssi_tot = fedpmt_ + statpmt_
g any_ssi = ssi_tot > 0 & ssi_tot < .

*INFLATE NEW VALUES
merge m:1 calendar_year using `cpi', keep(3) nogen
foreach x in fedpmt_ statpmt_ ssi_tot {
replace `x'=`x'*(cpiaucsl/236.9978) if inrange(calendar_year,2016,2018)
}

save "./processed/SSIpanel_cleaned_extrapolated", replace


*CLEAN VADC DATA INTO CALENDAR YEAR FORMAT
	 use "./raw/mepcom_vadc.dta", clear
 g calendar_year = year(filedt_s)
 keep pid calendar_year vadc_gross_amt vadc_iu vadc_cdr vadc_any_diag_ptsd_sc
 replace vadc_gross_amt = vadc_gross_amt*12
 replace vadc_iu = 0 if vadc_cdr<60
 g vadc_worklim = vadc_iu == 1 | (vadc_cdr >=100 & vadc_cdr<.)
 g vadc_100 = vadc_cdr == 100
 g any_ptsd = vadc_any_diag_ptsd_sc==1
 drop  vadc_cdr vadc_any_diag_ptsd_sc
 duplicates drop pid calendar_year, force
 isid pid calendar_year
save "./processed/VADCpanel_cleaned", replace



*------------------------*
* CREATE NSC GRAD DATA
*-------------------------*
use "./raw/mepcom_nsc_grad_v4_cy", clear
rename college_cd code
merge m:1 code using "./raw/NSClist_JAN2020_collapsed"
drop _merge
preserve
use "./processed/cleaned_mepcom.dta", clear
keep pid firstafqtdt
isid pid
tempfile a
save `a', replace
restore

merge m:1 pid using `a'
keep if _merge == 3 

g ym1 = ym(yr_begin,month_begin)
g ym2 = ym(year(firstafqtdt),month(firstafqtdt))
g covered_institution = ym1 < ym2 
replace covered_institution = . if ym2 == .

collapse (max) covered_institution assc bach mstr dctr, by(pid grad_cy)
foreach var of varlist assc bach mstr dctr {
replace `var' = 1 if `var' >= 1 & `var' < .
}

g anygrad = 1
rename grad_cy calendar_year
g mstrdctr = mstr|dctr
fsum *
fsum * if anygrad
keep if calendar_year >= 1999
keep if calendar_year <= 2018

g assoconly = assc == 1 
g assocplus = assc == 1 | bach == 1 | mstr == 1 | dctr == 1
g bachplus = bach == 1 | mstr == 1 | dctr == 1

keep pid calendar_year assocplus bachplus assoconly
save "./processed/NSCgradpanel_cleaned7_nocens", replace


*------------------------*
* CREATE NSC MAJORXGRAD DATA
*-------------------------*
use "./raw/mepcom_nsc_grad_v4_major_mar2022_nopii", clear
rename college_cd code

merge m:1 code using "./raw/NSClist_JAN2020_collapsed"
drop _merge

preserve
use "./processed/cleaned_mepcom.dta", clear
keep pid firstafqtdt
isid pid
tempfile a
save `a', replace
restore

merge m:1 pid using `a'
keep if _merge == 3 


g ym1 = ym(yr_begin,month_begin)
g ym2 = ym(year(firstafqtdt),month(firstafqtdt))
g covered_institution = ym1 < ym2 
replace covered_institution = . if ym2 == .

foreach var of varlist assc bach mstr dctr stem_dhs stem_va {
replace `var' = 1 if `var' >= 1 & `var' < .
}

summ assc bach mstr dctr stem_dhs stem_va

g asscXstemd = (assc ==1)& stem_dhs ==1
g bachXstemd = (bach ==1)& stem_dhs ==1
g mstrXstemd = (mstr == 1) & stem_dhs ==1 
g dctrXstemd = (dctr == 1) & stem_dhs ==1 

g asscXstemv = (assc ==1)& stem_va ==1
g bachXstemv = (bach ==1)& stem_va ==1
g mstrXstemv = (mstr == 1) & stem_va ==1 
g dctrXstemv = (dctr == 1) & stem_va ==1

g asscXstemn = (assc ==1)& stem_nsf ==1
g bachXstemn = (bach ==1)& stem_nsf ==1
g mstrXstemn = (mstr == 1) & stem_nsf ==1 
g dctrXstemn = (dctr == 1) & stem_nsf ==1 

collapse (max) covered_institution  asscX* bachX* mstrX* dctrX*, by(pid grad_cy) // assc bach mstr dctr

g anygrad = 1
rename grad_cy calendar_year


keep if calendar_year >= 1999
keep if calendar_year <= 2018

g assoconlyXstemd = (asscXstemd == 1)
g assocplusXstemd = (asscXstemd == 1 | bachXstemd == 1 | mstrXstemd == 1 | dctrXstemd == 1)
g bachplusXstemd = (bachXstemd == 1 | mstrXstemd == 1 | dctrXstemd == 1) 

g assoconlyXstemv = (asscXstemv == 1)
g assocplusXstemv = (asscXstemv == 1 | bachXstemv == 1 | mstrXstemv == 1 | dctrXstemv == 1)
g bachplusXstemv = (bachXstemv == 1 | mstrXstemv == 1 | dctrXstemv == 1) 

g assoconlyXstemn = (asscXstemn == 1)
g assocplusXstemn = (asscXstemn == 1 | bachXstemn == 1 | mstrXstemn == 1 | dctrXstemn == 1)
g bachplusXstemn = (bachXstemn == 1 | mstrXstemn == 1 | dctrXstemn == 1) 

keep pid calendar_year assoconlyXstemd assocplusXstemd bachplusXstemd assoconlyXstemv assocplusXstemv bachplusXstemv assoconlyXstemn assocplusXstemn bachplusXstemn
save "./processed/NSCgradmajorpanel_cleaned7_nocens", replace


*------------------------*
* CREATE NSC ENROLLMENT DATA
*-------------------------*

use "./raw/mepcom_nsc_enroll_v4_cy", clear
rename college_cd code
merge m:1 code using "./raw/NSClist_JAN2020_collapsed"
drop _merge

preserve
use "./processed/cleaned_mepcom.dta", clear
keep pid firstafqtdt
isid pid
tempfile a
save `a', replace
restore

merge m:1 pid using `a'
keep if _merge == 3 

g ym1 = ym(yr_begin,month_begin)
g ym2 = ym(year(firstafqtdt),month(firstafqtdt))

g covered_institution = ym1 < ym2 
replace covered_institution = . if ym2 == .

collapse (max) covered_institution enroll_days-enroll_stat_025, by(pid enroll_cy)
foreach var of varlist enroll_inst_4yr_prv_fp-enroll_stat_025 {
replace `var' = 1 if `var' >= 1 & `var' < .
}

g enroll_modplus = enroll_pmod | enroll_pvry | enroll_pmst
g enroll_minless = enroll_pmin | enroll_pnon
g enroll_4_np = enroll_inst_4yr_prv_np  | enroll_inst_4yr_pub

rename enroll_cy calendar_year
g postsecondary  = 1 
rename ( enroll_inst_4yr_prv_fp     enroll_inst_4yr_prv_np  enroll_inst_4yr_pub enroll_4_np enroll_inst_2yr_pub enroll_modplus enroll_minless ) (e4privfp e4privnp e4pub e4np e2pub emodplus eminless) 
fsum *
fsum * if postsecondary
keep if calendar_year >= 1999
keep if calendar_year <= 2018
		g e2 =  enroll_inst_2yr_prv_fp | enroll_inst_2yr_prv_np | e2pub 
		g e4 = e4privfp | e4privnp | e4pub

keep pid calendar_year postsecondary e4 e4privfp e4np  e2 emodplus eminless
save "./processed/NSCpanel_cleaned7_nocens", replace


*------------------------*
* LOAD BASELINE SAMPLE DATA AND MERGE ON
*-------------------------*


 use "./processed/cleaned_mepcom.dta", clear	
drop if firstafqt == 60 & firstafqt_fy == 1990
gen firstyear = year(firstafqtdt)
replace firstafqt_fy = 3000 if firstafqt_fy == 2004.75		
keep if inrange(firstyear,1990,2011)

gen lo_educ = (educ_lessthanhs + educ_ged)
gen hi_educ = (educ_some_coll + educ_coll_grad)

*BRING IN DATA	
		global start = -2 
		global end = 19 

	forval y=$start/$end {
		local ytitle = `y'
		if `y' == -2 local ytitle m2
		if `y' == -1 local ytitle m1
		gen calendar_year = firstyear+`y'
		g year = calendar_year
		merge m:1 year using  "./raw/cpi.dta", nogen keep(1 3)
		cap drop year		
		merge m:1 pid calendar_year using "./processed/VADCpanel_cleaned", nogen keep(1 3)
		merge m:1 pid calendar_year using "./processed/SSDIpanel_cleaned_extrapolated", nogen keep(1 3)
		merge m:1 pid calendar_year using "./processed/SSIpanel_cleaned_extrapolated", nogen keep(1 3)
		merge m:1 pid calendar_year using "./processed/NSCgradpanel_cleaned7_nocens", nogen keep(1 3)
		merge m:1 pid calendar_year using "./processed/NSCpanel_cleaned7_nocens", nogen keep(1 3)
		merge m:1 pid calendar_year using "./processed/NSCgradmajorpanel_cleaned7_nocens",  nogen keep(1 3)
		
		foreach x in calendar_year assoconly assocplus bachplus assoconlyXstemd assocplusXstemd bachplusXstemd assoconlyXstemv assocplusXstemv bachplusXstemv assoconlyXstemn assocplusXstemn bachplusXstemn postsecondary e4 e4privfp e4np  e2 emodplus eminless vadc_gross_amt vadc_worklim vadc_iu cpiaucsl dib_aux_tot dib_prim_tot any_dib dib_tot any_ssi ssi_tot vadc_100 any_ptsd {
			rename `x' `x'_`ytitle'
		}

		foreach x in  vadc_gross_amt dib_aux_tot dib_prim_tot dib_tot ssi_tot { 
			*adjust for inflation
			replace `x'_`ytitle' = `x'_`ytitle'*251.1041667/cpiaucsl_`ytitle'
			*force to 0 if during period with outcome data, force to missing otherwise
			replace `x'_`ytitle' = 0 if `x'_`ytitle'==. & calendar_year_`ytitle'>=1999 
			
			replace `x'_`ytitle' = . if calendar_year_`ytitle'<1999 | calendar_year_`ytitle'>2018
			*winsorize income variables at 0 and 99th percentile
			replace `x'_`ytitle' = 0 if `x'_`ytitle'<0
		}


		foreach x in vadc_worklim vadc_iu any_dib any_ssi vadc_100 any_ptsd assoconly assocplus bachplus assoconlyXstemd assocplusXstemd bachplusXstemd assoconlyXstemv assocplusXstemv bachplusXstemv assoconlyXstemn assocplusXstemn bachplusXstemn postsecondary e4 e4privfp e4np  e2 emodplus eminless {
			replace `x'_`ytitle' = 0 if `x'_`ytitle'==. & inrange(calendar_year_`ytitle',1999,2018)
			replace `x'_`ytitle' = . if calendar_year_`ytitle'<1999 | calendar_year_`ytitle'>2018
		}
		
	}	// end loop over relative years
	
	
cap drop calendar_year*


keep pid firstafqt firstafqtdt afqt_pctl afqt_dt_s  access firstyear firstafqt_fy hor_state age_days male white black hisp educ_inhs lo_educ educ_hsdip hi_educ assoconly* assocplus* bachplus* postsecondary* e4* e4privfp* e4np*  e2* emodplus* eminless* vadc_gross_amt* vadc_worklim* vadc_iu* any_dib* dib_tot* dib_aux_tot* dib_prim_tot* any_ssi* ssi_tot* vadc_100* any_ptsd* 


forval y=$start/$end {
		local ytitle = `y'
		if `y' == -2 local ytitle m2
		if `y' == -1 local ytitle m1
		
		
				g anyvadc_`ytitle' = vadc_gross_amt_`ytitle' > 0 
				replace anyvadc_`ytitle' = . if vadc_gross_amt_`ytitle' == . 

		g any_dis_`ytitle' = anyvadc_`ytitle' > 0 | any_dib_`ytitle' > 0 | any_ssi_`ytitle'>0
		replace any_dis_`ytitle' = . if anyvadc_`ytitle' == . | any_dib_`ytitle' == . | any_ssi_`ytitle'==.
		
		g dis_tot_`ytitle' = vadc_gross_amt_`ytitle' + dib_tot_`ytitle' + ssi_tot_`ytitle'

		g any_worklim_`ytitle' = vadc_worklim_`ytitle' > 0 | any_dib_`ytitle' > 0 | any_ssi_`ytitle'>0
		replace any_worklim_`ytitle' = . if vadc_worklim_`ytitle' == . | any_dib_`ytitle' == .  | any_ssi_`ytitle'==.
				
			g temp_`ytitle' =  vadc_gross_amt_`ytitle'
			replace temp_`ytitle' = 0 if vadc_worklim_`ytitle' == 0
		g worklim_tot_`ytitle' = temp_`ytitle' + dib_tot_`ytitle' + ssi_tot_`ytitle'
		
		g any_ssissdi_`ytitle' = any_dib_`ytitle' > 0 | any_ssi_`ytitle'>0
		replace any_ssissdi_`ytitle' = . if any_dib_`ytitle' ==.  | any_ssi_`ytitle' == . 

		g ssissdi_tot_`ytitle' = dib_tot_`ytitle' + ssi_tot_`ytitle'

		rename any_dib_`ytitle' any_ssdi_`ytitle'
		rename dib_tot_`ytitle' ssdi_tot_`ytitle' 
				
}


drop temp_* 
compress 

save "./processed/cleaned_analysis_data_NSC_VADC_v7", replace



